package com.studio.bill.swiperecyclerdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.studio.bill.swiperecyclerdemo.R;
import com.studio.bill.swiperecyclerdemo.bean.NonInsuranceBean;
import com.studio.bill.swiperecyclerdemo.util.libimage.ImageConfig;
import com.studio.bill.swiperecyclerdemo.util.libimage.ImageLoader;

import java.util.List;

/**
 * Created by pc on 2017/3/14.
 */

public class RecyclerViewDemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<NonInsuranceBean> mDatas;

    //普通Item View
    private static final int TYPE_ITEM = 0;

    //底部FootView
    private static final int TYPE_FOOTER = 1;

    //已经到底了
    public static final int PULLUP_LOAD_MORE = 0;

    //正在加载中
    public static final int LOADING_MORE = 1;

    //上拉加载状态-默认为0
    private int load_more_status = 0;

    public RecyclerViewDemoAdapter(Context mContext, List<NonInsuranceBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    //创建单个item控件的时候调用该方法
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //super.onCreateViewHolder(parent,viewType);
        Log.d("swipe_demo","on create view holder");
        //进行判断显示类型，来创建返回不同的View
        if (viewType == TYPE_ITEM) {
            //设置普通item的布局文件
            //这里必须设置parent参数，否则item里面的居中，宽高等属性不生效
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service_insurance_recommend, parent,false);
            return new ItemHolder(view);
        }
        // type == TYPE_FOOTER 返回footerView
        else if (viewType == TYPE_FOOTER) {
            //设置上拉时最底部item的布局文件
            //这里必须设置parent参数，否则item里面的居中，宽高等属性不生效
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_footer, parent,false);
            return new FooterHolder(view);
        }
        return null;
    }

    //增加某个空间到recyclerview的时候调用该方法
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.d("swipe_demo","on bind view holder called position : " + position);
        if (holder instanceof ItemHolder) {

            ItemHolder itemHolder = (ItemHolder) holder;
            NonInsuranceBean bean = mDatas.get(position);
            Log.i("ds", "zll ---- ssss position:"+position+",size:"+mDatas.size()+",bean.img:"+bean.img);
            ImageLoader.getInstance().displayImage(bean.img, itemHolder.ivImg, ImageConfig.getRoundCornerImgOptions(R.drawable.home_default,3));

        } else if (holder instanceof FooterHolder) {
            FooterHolder footHolder = (FooterHolder) holder;
            //这句如果不注释，那么底部就会有添加的一个item
            footHolder.linearLayout.setVisibility(View.GONE);
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footHolder.linearLayout.setVisibility(View.GONE);
                    //footHolder.textView.setText("已经到底了");
                    break;
                case LOADING_MORE:
                    footHolder.linearLayout.setVisibility(View.VISIBLE);
                    footHolder.textView.setText("正在加载更多数据...");
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size() + 1;
    }


    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    //自定义item类型
    class ItemHolder extends RecyclerView.ViewHolder {
        private ImageView ivImg;

        public ItemHolder(View itemView) {
            super(itemView);
            ivImg = (ImageView) itemView.findViewById(R.id.insurance_recommend_iv);
        }
    }

    //自定义item类型
    class FooterHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public LinearLayout linearLayout;

        public FooterHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_item_recycler_footer);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_recycler_footer);
        }
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }
}